/* eslint-disable */
const { list } = require('./src/mock')
const bodyParser = require('body-parser')
const car = require('./src/mock/car.json')
module.exports = {
	devServer: {
		before(app) {
			app.use(bodyParser.json())

			// 首页列表
			app.get('/api/list', (req, res) => {
				const { pageSize = 8, pageCode = 1 } = req.query;

				res.send({
					code: 200,
					data: list.slice((pageCode - 1) * pageSize, pageCode * pageSize),
					total: list.length,
					isFinish: pageSize * pageCode > list.length
				})
			})

			// 详情页
			app.post('/api/list/detail', (req, res) => {
				const { id } = req.body;
				const item = list.find(v => v.id === id)

				if (item) {
					res.send({
						code: 200,
						data: item
					})
				} else {
					res.send({
						code: 404,
						data: null
					})
				}
			})

			// 登录接口
			app.post('/api/login', (req, res) => {
				const { user, pwd } = req.body;

				if (user === 'admin' && pwd === 'admin') {
					res.send({
						code: 200,
						data: {
							user: 'admin',
							token: Math.random().toString().slice(2)
						}
					})
				} else {
					res.send({
						code: 403
					})
				}
			})

		},
		// 代理到懂车帝服务器


		proxy: {
			'/dcd': {
				target: 'http://m.dcdapp.com/motor/brand/m',
				pathRewrite: { '^/dcd': '' },
			},
		},
	}
}